/**
 * Created by dwj on 2024/5/16.
 * 消息通讯
 */
Rsd.define('Rsd.data.Message', {
    extend: 'Rsd.common.Object',
    xtype: 'message', 
    /**
     * 
     */
    header: "RSD_MSG",
    /**
     * 
     */
    version: "1.0",
    /**
     * 
     */
    authorization: "",
    /**
      * 
      */
    messageId: '',
    /**
    * 
    */
    merchId: 0,

    /**
     * 
     */
    receipt: 0,
    /**
     * 
     */
    senderId: "",
    /**
   * 
   */
    contentType: '',

    /**
     * 
     */
    bodyFormat: "Json",

    /**
     * 
     */
    body: '',

    /**
     * 
     */
    from: "Client",
    /**
      * 发送次数
      */
    count: 0,
    /**
     * 
     */
    signature: "",
    /**
     * 
     */
    time: Date.now(),
    /**
     * 
     */
    end: "====",
    /*
    *
    * */
    constructor: function Message(config) {

        config = config || {}; 
        this.apply(config);  
    },
    /**
     * 
     * @param {*} body 
     */
    setBody:function setBody(body)
    { 
        if(!body.key)
        {
            body.key = this.messageId||Rsd.getUUID(); 
        }
        if(body.receipt===undefined)
        {
            body.receipt= this.receipt||false;
        }
        this.messageId = body.key;
        this.receipt = body.receipt;
        var list=[];
        for(var i in body)
        {
            if(body[i]==undefined || body[i]==null)
            {
                continue;
            }
            list.push(i+":"+body[i]);
        }
        
        var str = "{" + list.join(',') +"}";
        //console.log(str);
        str = Rsd.encodeBase64(str);
        //console.log(str);
        this.body=str;
    }, 
    /**
     * 
     */
    getBody:function getBody()
    {
        var utf8Decoder = new TextDecoder('utf-8');
        var str = utf8Decoder.decode(Rsd.decodeBase64(this.body));
        //console.log(str);
        str = str.substring(str.indexOf('{')+1,str.lastIndexOf('}'));
        //console.log(str);
        var list = str.split(',');
        var data={};
        for(var i=0;i< list.length;i++)
            {
                var item = list[i];
                if(Rsd.isEmpty(item))
                {
                    continue;
                }
                var key_value = item.split(':');
                var key = key_value[0];
                var value = key_value.length>1?key_value[1]:null;
                data[key.toFirstLowerCase()]=value;
            }

        //console.log(data); 
        return data
    },
    /**
     * 生成签名
     */
    generateSignature:function generateSignature()
    {

    },
    /**
     * 
     */
    toBase64String:function toBase64String()
    {
        return Rsd.encodeBase64(this.toString());
    },
    /**
     * 
     */
    toString:function toString()
    {
        var list=[];
       
        list.push("Header:"+this.header);
        list.push("Authorization:"+this.authorization);
        list.push("MerchId:"+this.merchId);
        list.push("SenderId:"+this.senderId);
        list.push("Receipt:"+this.receipt);
        list.push("MessageId:"+this.messageId);
        list.push("From:"+this.from);
        list.push("Version:"+this.version);
        list.push("Time:"+this.time);
        list.push("Count:"+this.count);
        list.push("ContentType:"+this.contentType);
        list.push("BodyFormat:"+this.bodyFormat);
        list.push("Body:"+this.body);
        list.push("Signature:"+this.signature);
        list.push("End:"+this.end);
         
        return "{" + list.join(',')+"}";
    }

}, function (type) {

});